package cn.ichiva.puppet.example.base.p1;

import javafx.fxml.FXML;
import javafx.fxml.impl.AbstractPuppetComponent;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 条件渲染,事件绑定
 */
public class L5ForComponent extends AbstractPuppetComponent {
    @FXML
    public List<Actor> actors = new ArrayList<>(Arrays.asList(
            new Actor(1L,"张飞",28),
            new Actor(2L,"关羽",32),
            new Actor(3L,"刘备",36)
    ));

    @Override
    public CharSequence render() {
        imports(VBox.class, Label.class, Button.class);
        return """
                <VBox>
                    <Label text="${controller.actors.get(index).getName()}" for="index : actors" />
                    <Button text="添加群众演员" onAction="#addActor" />
                </VBox>
                """;
    }

    @FXML
    void addActor(){
        long id = System.currentTimeMillis();
        actors.add(new Actor(id,"群众" + id,22));
        update();
    }

    public static class Actor{
        Long id;
        String name;
        int age;

        public Actor() {
        }

        public Actor(Long id, String name, int age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }
}
